﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.IO;
using System.Threading;

namespace Capokun
{
    public class LocalizedViewEngine : RazorViewEngine
    {
        public LocalizedViewEngine()
        {
            this.ViewLocationFormats = new string[]
      {
         //{0} - Culture Name, {1} - Controller, {2} - Page, {3} Extension (aspx/ascx)
         "~/Views/{0}/{1}/{2}{3}",
         "~/Views/en-US/{1}/{2}{3}",
         "~/Views/Shared/{1}/{2}{3}",
         "~/Views/Shared/{2}{3}"
      };
        }

        private ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache, string extension)
        {
            //Get the current culture and name of it.
            var cultureName = controllerContext.RouteData.GetRequiredString("culture");
            ///set culture for ressourcemanager to know which language to use
            //Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureName);

            //Get the name of the controller from the path
            var controller = controllerContext.RouteData.GetRequiredString("controller");

            //Create the key to use the culture for caching purposes            
            var keyPath = Path.Combine(cultureName, controller, viewName + extension);

            //Try the cache            
            if (useCache)
            {
                //If using the cache, check to see if the location is cached.                
                var cacheLocation = this.ViewLocationCache.GetViewLocation(controllerContext.HttpContext, keyPath);
                if (!string.IsNullOrWhiteSpace(cacheLocation))
                    return new ViewEngineResult(CreateView(controllerContext, cacheLocation, masterName), this);
            }

            //Remember the attempted paths, if not found display the attempted paths in the error message.            
            var attempts = new List<string>();

            //for each of the paths defined, format the string and see if that path exists. When found, cache it.            
            foreach (var rootPath in this.ViewLocationFormats)
            {
                var currentPath = string.Format(rootPath, cultureName, controller, viewName, extension);
                if (this.FileExists(controllerContext, currentPath))
                {
                    this.ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, keyPath, currentPath);
                    return new ViewEngineResult(CreateView(controllerContext, currentPath, masterName), this);
                }
                //If not found, add to the list of attempts.                
                attempts.Add(currentPath);
            }

            //if not found by now, simply return the attempted paths.            
            return new ViewEngineResult(attempts);
        }

        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            return FindView(controllerContext, viewName, masterName, useCache, ".cshtml");
        }

        //public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        //{
        //    return FindView(controllerContext, partialViewName, string.Empty, useCache, ".ascx");
        //}

    }
}